<?php


namespace app\plugin\controller\merchant\com;


use app\admin\controller\com\Admin;
use app\common\controller\AppCommon;
use app\plugin\controller\merchant\service\MerchantService;
use app\service\PluginsService;
use think\Controller;
use think\Request;

class MerchantManager extends Controller
{
    public $tag = 'merchant';
    public $param = null;
    public $m_uid = '';


    public $plugin = null;
    public $store_num = 0;

    public function __construct(Request $request = null)
    {
        parent::__construct($request);

        MerchantService::ini_sql();
        $this->m_uid = session('my_merchant_uid', '', 'bs_merchant_');
        if (!$this->m_uid) {
            if (IS_AJAX || IS_POST) {
                data_return('登录超时', 401, ['url' => url('/plugin/' . $this->tag . '.account/login')]);
            }
            $domain = request()->domain();
            $currentUrl = $domain . $_SERVER['REQUEST_URI'];
            cookie('my_merchant_backurl', $currentUrl, 3600);
            return $this->error('登录超时', url('/plugin/' . $this->tag . '.account/login'), '', 1);
        }
        $this->store_num = AppCommon::data_get_column('stores', ['merchant_uid' => $this->m_uid], 'store_num');

        if (!bs_p($this->tag)) {
            return $this->error('应用已关闭');
        }

        $this->param = input();
        $info = PluginsService::get_plugin_info($this->tag);
        if ($info['code'] == 0) {
            $this->plugin = $info['data'];
        }

        $this->assign('menu', $this->get_config('menu'));
    }

    private function get_config($key = '')
    {
        $config = require __DIR__ . '/config.php';
        if (!$key) {
            return $config;
        }
        return !empty($config[$key]) ? $config[$key] : [];

    }

    //检查当前登录商家账号是否正常
    protected function check_account()
    {
        if (!$this->m_uid) {
            return data_return_arr('请先登录', -1);
        }
        //0-禁用，1-启用，-1-待审核，-2-已驳回
        $account = AppCommon::data_get('plugin_merchant', ['uid' => $this->m_uid], 'status,id');
        if (empty($account)) {
            return data_return_arr('登录超时', -1);
        } elseif ($account['status'] == 0) {
            return data_return_arr('账号异常，请联系管理员'.$account['id'], -1);
        } elseif ($account['status'] == -1) {
            return data_return_arr('账号审核过后才可以使用本功能', -1);
        } elseif ($account['status'] == -2) {
            return data_return_arr('账号被驳回了，重新提交一下审核资料，过后才可以使用本功能', -1);
        }
        return data_return_arr('success', 0);
    }
}